## This file runs all the code necessary for replicating the empirical example
## on voter turnout appearing in Chiu and Xu (2021)

# please set path and source helper.r
setwd(rep_path)
source("helper.R")


## Part I: Packages and data -----------------------
# Load packages
require(dplyr)
require(BRS)
require(QCA)
require(ggplot2)
require(cowplot)
require(circlize)
require(RColorBrewer)
require(prettyGraphs)
require(Rtsne)
require(kableExtra)
require(rpart)
require(rpart.plot)
require(randomForest)
require(glmnet)
require(gtools)
require(ggpattern)

# Load and format data and create necessary labels and other objects
source("vote/vote_make-data.R")


## Part II: Run BRS -----------------------
# Takes a while to run. 
# Resulting rule sets are supplied as part of replication materials for convenience. (Part III)
# The code in the following chunk will produce "vote_out_pois.rda" (supplied)
if (FALSE) {
  reticulate::use_condaenv("BRS_conda")  ## use this conda environment for BRS

  # Set number of cores
  numCores <- 4
  registerDoParallel(numCores)
  # Run BRS with bootstrapping 
  source("vote/vote_pois.R")
}


## Part III: Make graphs/tables -----------------------

# requires "vote/out/vote_out_pois.rda"

# Make figures that appear in the paper
# figures will be saved in the "figures" folder
source("vote/vote_bar.R") # Figure 4
source("vote/vote_chord.R") # Figure 5(a)
source("vote/vote_tsne.R") # Figure 5(b)

# Tree based and regression analyses in appendix
source("vote/vote_trees.R") # Figure A2
source("vote/vote_lasso.R") # Figure A3


